package cn.edu.zjicm.wordsnet_d.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import cn.edu.zjicm.wordsnet_d.datapersistent.AppPreference;
import cn.edu.zjicm.wordsnet_d.util.NightModeUtil;

public class Cells extends View {
	private Context context;
	private int rowNum = 7;
	private int colNum = 3;
	private float startX = 0;
	private float startY = 0;
	private float firstCellWidth;
	private float otherCellWidth;
	private float allCellHeight;
	private String[] firstRow = { "合计", "今天新增" };
	private String[] firstCol = { "单词", "太简单（熟悉度5）", "熟词（熟悉度4）", "生词-熟悉度3", "生词-熟悉度2", "生词-熟悉度1", "未学" };
	private float hSpace = 15;
	private float wSpace = 15;

	// 必须带有AttributeSet，否则会报错
	public Cells(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		drawCells(canvas);
		drawFirstColumnBackground(canvas);
		drawOtherCells(canvas);
		drawContents(canvas);
	}

	private void drawFirstColumnBackground(Canvas canvas) {
		// TODO Auto-generated method stub

		// 向内绘制圆角型矩形的
		Paint mPaint = new Paint();
		mPaint.setStyle(Style.FILL);
		mPaint.setAntiAlias(true);
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(56, 59, 65));
		else
			mPaint.setColor(Color.WHITE);
		RectF r1 = new RectF(startX + 2, startY + 2, startX + getWidth() - 2, startY + getHeight() - 2);
		canvas.drawRoundRect(r1, 10, 8, mPaint);

		// 绘制第一行的圆角矩形
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(67, 72, 85));
		else
			mPaint.setColor(Color.rgb(244, 244, 244));
		RectF r2 = new RectF(startX + 2, startY + 2, startX + getWidth() - 2, startY + allCellHeight + 20);
		canvas.drawRoundRect(r2, 10, 8, mPaint);

		// 将绘制第一行圆角矩形产生的多余部分去掉
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(56, 59, 65));
		else
			mPaint.setColor(Color.WHITE);
		canvas.drawRect(startX + 2, startY + allCellHeight, getWidth() - 2, startY + allCellHeight * 2, mPaint);
	}

	private void drawCells(Canvas canvas) {
		// TODO Auto-generated method stub
		firstCellWidth = getWidth() / 2;
		otherCellWidth = getWidth() / 4;
		allCellHeight = getHeight() / 7;
		hSpace = allCellHeight / 5 * 2;
		wSpace = otherCellWidth / 11 * 3;
		Log.i("getWidth", getWidth() + "");
		Log.i("getHeight", getHeight() + "");

		// 绘制圆角型矩形的
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);

		if (NightModeUtil.isNightModeNow()) {
			mPaint.setColor(Color.rgb(31, 32, 35));
		} else
			mPaint.setColor(Color.rgb(192, 192, 192));
		mPaint.setStyle(Style.FILL);
		RectF r = new RectF(startX, startY, getWidth(), getHeight());
		canvas.drawRoundRect(r, 10, 8, mPaint);

		/*
		 * // 绘制矩形外部线框 canvas.drawColor(Color.WHITE); canvas.drawRect(startX,
		 * startY, getWidth(), getHeight(), mPaint); canvas.drawRect(startX+1,
		 * startY+1, getWidth()-1, getHeight()-1, mPaint);
		 */
	}

	// 设置画笔
	private Paint setPaint() {
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth(0);
		mPaint.setStyle(Style.STROKE);
		mPaint.setColor(Color.rgb(192, 192, 192));
		return mPaint;
	}

	private void drawOtherCells(Canvas canvas) {
		Paint mPaint = setPaint();
		if (NightModeUtil.isNightModeNow()) {
			mPaint.setColor(Color.rgb(31, 32, 35));
		}
		// 绘制横线
		for (int i = 1; i < rowNum; i++) {
			float start_x = startX;
			float start_y = startY + allCellHeight * i;
			if (i > 3 && i < rowNum - 1) {
				canvas.drawLine(start_x, start_y, getWidth() - otherCellWidth, start_y, mPaint);
			} else
				canvas.drawLine(start_x, start_y, getWidth(), start_y, mPaint);
		}

		// 绘制竖线
		for (int i = 0; i < colNum - 1; i++) {
			float start_x = startX + firstCellWidth + otherCellWidth * i;
			float stop_x = start_x;
			float start_y = startY;
			float stop_y = startY + allCellHeight * rowNum;
			canvas.drawLine(start_x, start_y, stop_x, stop_y, mPaint);
		}
	}

	private void drawContents(Canvas canvas) {
		Paint mPaint = setPaint();
		mPaint.setTextSize(otherCellWidth / 5);
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(166, 175, 188));
		else
			mPaint.setColor(Color.rgb(51, 51, 51));

		// drawFirstRow
		for (int i = 1; i < colNum; i++) {
			float start_x = startX + wSpace + firstCellWidth + otherCellWidth * (i - 1);
			float start_y = startY + allCellHeight - hSpace;
			if (i == (colNum - 1))
				start_x -= wSpace / 3 * 2;
			canvas.drawText(firstRow[i - 1], start_x, start_y, mPaint);
		}

		// drawFirstCol
		for (int i = 1; i <= rowNum; i++) {
			float start_x = startX + wSpace;
			float start_y = startY + allCellHeight * i - hSpace;
			if (i != 1) {
				mPaint.setTextSize(wSpace / 3 * 2);
				if (i == 2) {
					mPaint.setColor(Color.rgb(23, 160, 96));
				} else if (i == 3)
					mPaint.setColor(Color.rgb(85, 180, 202));
				else if (i >= 4 && i <= 6)
					mPaint.setColor(Color.rgb(222, 117, 92));
				else if (i == 7)
					mPaint.setColor(Color.rgb(116, 177, 132));
			}
			canvas.drawText(firstCol[i - 1], start_x, start_y, mPaint);
		}

		// draw words
		if (NightModeUtil.isNightModeNow())
			mPaint.setColor(Color.rgb(166, 175, 188));
		else
			mPaint.setColor(Color.rgb(51, 51, 51));
		for (int i = 2; i <= rowNum; i++) {
			float start_x = startX + wSpace + firstCellWidth;
			float start_y = startY + allCellHeight * i - hSpace;
			if (i == 4) {
				int count = AppPreference.getNewWordsFmDegree(context, 4) + AppPreference.getNewWordsFmDegree(context, 3);
				canvas.drawText(count + "", start_x, start_y, mPaint);
			} else if (i > 4) {
				canvas.drawText(AppPreference.getNewWordsFmDegree(context, 7 - i) + "", start_x, start_y, mPaint);
			} else {
				canvas.drawText(AppPreference.getNewWordsFmDegree(context, 8 - i) + "", start_x, start_y, mPaint);
			}

			// Log.i(i + "单词数量:", "" +
			// AppPreference.getNewWordsFmDegree(context, 8 - i));
		}

		// new add today
		int todayWords = 0, yestdayWords = 0, newAddWords;
		float start_x = startX + wSpace + firstCellWidth + otherCellWidth;
		for (int i = 2; i <= rowNum; i++) {
			float start_y = startY + allCellHeight * i - hSpace;
			if (i == 2 || i == 3) {
				newAddWords = AppPreference.getNewWordsFmDegree(context, 8 - i) - AppPreference.getWordsFmDegree(context, 8 - i);
				mPaint = getPaintColor(mPaint, newAddWords);
				canvas.drawText(newAddWords + "", start_x, start_y, mPaint);
			} else if (i == rowNum) {
				newAddWords = AppPreference.getNewWordsFmDegree(context, 7 - i) - AppPreference.getWordsFmDegree(context, 7 - i);
				mPaint = getPaintColor(mPaint, newAddWords);
				canvas.drawText(newAddWords + "", start_x, start_y, mPaint);
			} else {
				todayWords += AppPreference.getNewWordsFmDegree(context, 7 - i);
				yestdayWords += AppPreference.getWordsFmDegree(context, 7 - i);

				if (i == 4) { // 这里还有个原来的“生词-熟悉度4”
					todayWords += AppPreference.getNewWordsFmDegree(context, 8 - i);
					yestdayWords += AppPreference.getWordsFmDegree(context, 8 - i);
				}
			}
		}
		newAddWords = todayWords - yestdayWords;
		mPaint = getPaintColor(mPaint, newAddWords);
		canvas.drawText(newAddWords + "", start_x, getHeight() / 8 * 5, mPaint);
	}

	public Paint getPaintColor(Paint mPaint, int newAddWords) {
		if (newAddWords == 0) {
			if (NightModeUtil.isNightModeNow())
				mPaint.setColor(Color.rgb(166, 175, 188));
			else
				mPaint.setColor(Color.rgb(51, 51, 51));
		} else if (newAddWords > 0)
			mPaint.setColor(Color.rgb(218, 107, 84));
		else
			mPaint.setColor(Color.rgb(23, 160, 96));
		return mPaint;
	}

}
